/*
 * IntRange.java
 */

package xq.util;

/**
 *
 * @author ThorntonRP
 */
public class IntRange {

	private final int start;
	private final int end;
	
	private final boolean startIncluded;
	private final boolean endIncluded;

	public IntRange(int start, boolean startIncluded, int end, boolean endIncluded) {
		if (start > end) {
			throw new IllegalArgumentException("end must be greater than or equal to start");
		}
		this.start = start;
		this.end = end;
		
		this.startIncluded = startIncluded;
		this.endIncluded = endIncluded;
	}

	public int getEnd() {
		return end;
	}

	public int getStart() {
		return start;
	}
	
	public int getWidth() {
		int result = end - start;
		if (!startIncluded) {
			result--;
		}
		if (!endIncluded) {
			result--;
		}
		return result;
	}
	
	public boolean contains(int n) {
		return ((startIncluded && (n >= start)) || (n > start)) &&
			   ((endIncluded   && (n <= end  )) || (n < end  ));
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final IntRange other = (IntRange) obj;
		if (this.start != other.start) {
			return false;
		}
		if (this.end != other.end) {
			return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 3;
		hash = 97 * hash + this.start;
		hash = 97 * hash + this.end;
		return hash;
	}

	@Override
	public String toString() {
		return String.format("[%1s, %2s]", start, end);
	}

	@Override
	protected Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException ex) {
			throw new IllegalStateException(ex);
		}
	}
}
